/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.service.jdbc;

import java.sql.Connection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.logging.*;

/**
 * 
 * @author 刘威
 */
public final class DBConnection {
	private static Log log = LogFactory.getLog(DBConnection.class);

	private static DBConnection instance;
	private ComboPooledDataSource dataSource;

	private DBConnection(String url) {
		try {
			dataSource = new ComboPooledDataSource();
			//dataSource.setUser(Configuration.getInstance().getValue("username"
			// ));
			// dataSource.setPassword(Configuration.getInstance().getValue(
			// "password"));
			dataSource.setJdbcUrl(JdbcConfig.getInstance().getValue("url")
					+ url);
			dataSource.setDriverClass(JdbcConfig.getInstance().getValue(
					"driverClassName"));
			dataSource.setInitialPoolSize(JdbcConfig.getInstance().getIntValue(
					"initialPoolSize"));
			dataSource.setMinPoolSize(JdbcConfig.getInstance().getIntValue(
					"minPoolSize"));
			dataSource.setMaxPoolSize(JdbcConfig.getInstance().getIntValue(
					"maxPoolSize"));
			dataSource.setMaxStatements(JdbcConfig.getInstance().getIntValue(
					"maxStatement"));
			dataSource.setMaxIdleTime(JdbcConfig.getInstance().getIntValue(
					"maxIdleTime"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static DBConnection getInstance(String url) {
		if (instance == null) {
			instance = new DBConnection(url);
		}

		return instance;
	}

	public Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("无法获取数据源... ...");
		}
	}

	public static void main(String[] args) {
	}
}
